package router

import (
	"lsgfish/server/internal/handler"
	"lsgfish/server/internal/middleware"
	"lsgfish/server/internal/tool"

	"github.com/gin-gonic/gin"
)

// NewV1Router 创建v1路由
func NewV1Router(r *gin.Engine) {
	// 创建控制器
	loginController := handler.NewLoginController()
	userController := handler.NewUserController()
	weatherController := handler.NewWeatherController()
	dbController := handler.NewGcodeController(tool.DB)

	// 注册路由组 - 保持原有路由不变
	v1Group := r.Group("/v1")
	{
		// 公共功能路由 - 按原路径
		{
			v1Group.GET("/weather", weatherController.GetWeather)
		}
		{
			// 登录相关路由 - 按原路径
			v1Group.GET("/register-verify-code", loginController.SendRegisterVerifyCode)
			v1Group.POST("/register", loginController.Register)
			v1Group.POST("/login", loginController.Login)
			v1Group.GET("/login-verify-code", loginController.GetVerifyCode)
			v1Group.POST("/verify-code-login", loginController.VerifyCodeLogin)
			v1Group.GET("/check-email-repeat/:email", loginController.CheckEmailRepeat)
			v1Group.POST("/logout", loginController.Logout)
		}

		// 用户相关路由 - 按原路径
		userGroup := v1Group.Group("/user", middleware.ValidateToken)
		{
			// 用户基本信息相关接口
			userGroup.POST("/change-password", userController.ChangePassword)
			userGroup.GET("/invit_code", userController.GetInvitationCode)
			userGroup.GET("/get", userController.GetUserInfo)

			// 用户管理相关接口
			userGroup.GET("/paginationList", userController.PaginationList)
			userGroup.POST("/create", userController.Create)
			userGroup.POST("/update", userController.Update)
			userGroup.POST("/delete", userController.Delete)
		}

		// 数据库管理相关路由 - 按原路径
		dbManageGroup := v1Group.Group("/db_manage", middleware.ValidateToken)
		{
			// 获取数据库结构树
			dbManageGroup.GET("/structure", dbController.GetDatabaseStructure)
			// 获取表结构详情
			dbManageGroup.GET("/table", dbController.GetTableStructure)
			// 生成表代码
			dbManageGroup.POST("/generate", dbController.GenerateTableCode)
		}

		// 下载路由 - 不需要token校验
		v1Group.GET("/download/:fileId/:fileName", dbController.DownloadGeneratedCode)
	}
}
